package main

import (
	"encoding/json"
	"fmt"
)

func test1() {
	//1.结构体的序列化
	stu := Student{
		Name:    "学生1",
		Age:     20,
		Address: "地址",
	}
	jsonBytes, err := json.Marshal(stu)
	if err != nil {
		fmt.Println("序列化发生错误")
		return
	}
	fmt.Printf("json:%v \n", string(jsonBytes)) //json:{"Name":"学生1","Age":20,"Address":"地址"}
}

func test2() {
	//2.map的序列化
	map1 := make(map[int]int)
	map1[1] = 1
	map1[2] = 2
	map1[3] = 3
	jsonBytes, err := json.Marshal(map1)
	if err != nil {
		fmt.Println("序列化发生错误")
		return
	}
	fmt.Printf("json:%v \n", string(jsonBytes)) //json:{"1":1,"2":2,"3":3}
}

func test3() {
	//3.map的序列化
	map2 := make(map[string]interface{})
	map2["id"] = 1
	map2["data"] = Student{
		Name:    "路飞",
		Age:     10,
		Address: "风车村",
	}
	jsonBytes, err := json.Marshal(map2)
	if err != nil {
		fmt.Println("序列化发生错误")
		return
	}
	fmt.Printf("json:%v \n", string(jsonBytes)) //json:{"data":{"Name":"路飞","Age":10,"Address":"风车村"},"id":1}
}

func test4() {
	//4.切片的序列化
	nums := []int32{1, 2, 3}
	jsonBytes, err := json.Marshal(nums)
	if err != nil {
		fmt.Println("序列化发生错误")
		return
	}
	fmt.Printf("json:%v \n", string(jsonBytes)) //json:[1,2,3]
}

func test5() {
	//5.结构体切片的序列化
	var stusSlice []Student
	stusSlice = append(stusSlice, Student{
		Name:    "路飞",
		Age:     10,
		Address: "风车村",
	})

	stusSlice = append(stusSlice, Student{
		Name:    "路飞2",
		Age:     12,
		Address: "风车村2",
	})
	jsonBytes, err := json.Marshal(stusSlice)
	if err != nil {
		fmt.Println("序列化发生错误")
		return
	}
	//json:[{"Name":"路飞","Age":10,"Address":"风车村"},{"Name":"路飞2","Age":12,"Address":"风车村2"}]
	fmt.Printf("json:%v \n", string(jsonBytes))
}

func test6() {
	//5.map的切片的序列化
	var mapSlice []map[string]interface{}
	m1 := make(map[string]interface{})
	m1["name"] = "张三"
	m1["age"] = 20
	mapSlice = append(mapSlice, m1)

	m2 := make(map[string]interface{})
	m2["name"] = "张三"
	m2["age"] = 20
	mapSlice = append(mapSlice, m2)
	jsonBytes, err := json.Marshal(mapSlice)
	if err != nil {
		fmt.Println("序列化发生错误")
		return
	}
	//json:[{"age":20,"name":"张三"},{"age":20,"name":"张三"}]
	fmt.Printf("json:%v \n", string(jsonBytes))
}

func test7() (string, error) {
	//1.结构体的序列化
	stu := Teacher{
		Name:    "老师1",
		Age:     20,
		Address: "地址",
	}
	jsonBytes, err := json.Marshal(stu)
	if err != nil {
		fmt.Println("序列化发生错误")
		return "", err
	}
	//{"老师的名字":"老师1","age":20,"addr":"地址"}
	fmt.Printf("json:%v \n", string(jsonBytes)) //json:{"Name":"学生1","Age":20,"Address":"地址"}
	return string(jsonBytes), nil
}

//演示json的序列化
func main() {
	jsonStr, err := test7()
	if err != nil {
		return
	}
	var result = Teacher{}
	err = json.Unmarshal([]byte(jsonStr), &result)
	if err != nil {
		fmt.Println("发生错误", err)
		return
	}
	fmt.Println("反序列化成功")
	fmt.Println(result)

}

//Student 学生
//属性全部都是可导出的，只有这样才可以json序列化
type Student struct {
	Name    string
	Age     int
	Address string
}

//以上存在一个问题，由于为了序列化，必须大写，但是其实json中字段小写才是最好的
//或者我们想序列化出来的字段名和属性名不同，就必须借助tag标签
type Teacher struct {
	Name    string `json:"老师的名字"` //甚至可以是中文
	Age     int    `json:"age"`
	Address string `json:"addr"`
}
